package com.wx.springboot.step9back.service.impl;

import com.wx.springboot.step9back.common.util.MyDate;
import com.wx.springboot.step9back.mapper.TeamMapper;
import com.wx.springboot.step9back.model.Team;
import com.wx.springboot.step9back.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("teamService")
public class TeamServiceImpl implements TeamService {

    @Autowired
    private TeamMapper teamMapper;

    @Override
    public List<Team> getTeamList() {
        return teamMapper.getTeamList();
    }

    @Override
    public Team getTeamByTeamCode(String teamCode) {
        return teamMapper.getTeamByTeamCode(teamCode);
    }

    @Override
    public String addTeam(Team team) {
        Team t = teamMapper.findTeamByTeamCode(team.getTeamCode());
        if (t== null){
        team.setCreateTime(MyDate.getDate());
        team.setUpdateTime(MyDate.getDate());
        return teamMapper.addTeam(team) == 1 ? "success":"false";
        }else {
            return "repeat";
        }
    }

    @Override
    public List<Team> getSelectList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return teamMapper.getSelectList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
        return teamMapper.getSelectCount(data);
    }

    @Override
    public int updateState(Team team) {
        team.setUpdateTime(MyDate.getDate());
        return teamMapper.updateState(team);
    }

    @Override
    public int updateTeam(Team team) {
        team.setUpdateTime(MyDate.getDate());
        return teamMapper.updateTeam(team);
    }

    @Override
    public int delTeam(Team team) {
        team.setUpdateTime(MyDate.getDate());
        return teamMapper.delTeam(team);
    }

    @Override
    public int updateMemberCount(Map param) {
        return teamMapper.updateMemberCount(param);
    }

    @Override
    public String batchDeletion(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        params.put("updateTime",MyDate.getDate());
        int result = teamMapper.batchDeletion(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public String removeLeader(String teamCode) {
        int result = teamMapper.removeLeader(teamCode);
        if (result > 0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public List<Team> getCheckTeam(List<Integer> ids) {
        Map params = new HashMap();
        params.put("ids",ids);
        return teamMapper.getCheckTeam(params);
    }
}
